import usePermissionStore from '@/store/modules/permission'
import useNavigate from '@/hooks/useNavigate'

type PageType = {
  label: string
  permission: string
  path: string
}

// 权限枚举
// 2: 短租,3: 普通长租,4: 车辆维修,5: 车辆保养,6: 车辆年检,11:临时占用,12:借出
const REDIRECT_ENUM = {
  2: { label: '短租详情', permission: 'svms:order:short-order.detail', path: '/short-order/detail' },
  3: { label: '长租详情', permission: 'svms:order:long-order.detail', path: '/long-order/detail' },
  4: { label: '维修详情', permission: 'svms:asstes:car:car-repair.detail', path: '/car-repair/detail' },
  5: { label: '保养详情', permission: 'svms:asstes:car:car-maintenance.detail', path: '/car-maintenance/detail' },
  6: { label: '年检详情', permission: 'svms:asstes:operation:car-inspect.detail', path: '/car-inspect/detail' },
  12: { label: '借调详情', permission: 'svms:asstes:operation:car-transfer.detail', path: '/car-transfer/detail' },
  // 此组件给排车图优先使用，后面的link从101开始
  // 101: { label: '调拨', value: 'svms:order:transfer' },
}

// 获取权限
function getPromission(page: PageType, disabled: boolean) {
  if (disabled) return false
  if (!page || !page.permission) {
    return false
  }
  const { resourcePermission } = usePermissionStore()
  return resourcePermission.includes(page.permission)
}

// 跳转链接组件，有权限展示位link，没有权限展示为文本
export default function OrderCodeLink(value: any) {
  const { id, orderCode, type, append, tag, disabled } = value
  const appendText = type && append ? `(${append})` : ''
  const tagText = type && tag ? tag : ''
  const page = REDIRECT_ENUM[type]
  const hasPromission = getPromission(page, disabled)

  if (hasPromission == false) {
    return (
      <span>
        {orderCode || '-'}
        {appendText ? <span>{appendText}</span> : null}
        {tagText ? (
         <a-tag size="small" color="red" style="margin-left: 2px">
            {tagText}
          </a-tag>
        ) : null}
      </span>
    )
  }

  const { navigateTo } = useNavigate()
  const toPage = () => {
    navigateTo(`${page.path}/${id}`, `${orderCode}-${page.label}`)
  }
  return (
    <span>
      <a-link onClick={toPage} type="link" style={{ fontSize: '12px' }}>
        {orderCode}
      </a-link>
      {appendText ? <span>{appendText}</span> : null}
      {tagText ? (
        <a-tag size="small" color="red" style="margin-left: 2px">
          {tagText}
        </a-tag>
      ) : null}
    </span>
  )
}
